import de.medieninf.ads.ADSTool;


public class Abgabe2aufg1 {
	
	public static void main(String[] args){
		
		String filename = "";
		String outfilename = "";
		
		if(args.length > 2 || args.length == 0)
			errorMsg();
		else if(args.length == 1){
			filename = args[0];
			outfilename = "out.pgm";
		}
		else{
			filename = args[0];
			outfilename = args[1];
		}
		
		int[] pgmIn = ADSTool.readPGM(filename);
		
		
		int [][] myFeld = getField(createBinaryImage(pgmIn));
		searchAreas(myFeld);
		//Aufruf Felderkennung
		
		
		ADSTool.savePGM("test.pgm", getArray(myFeld,pgmIn));
		
		int[] pgmOut = new int[pgmIn.length];
		int[][]fieldfield = getFieldField(ADSTool.readPGM(filename));
		int[][]b = createBinaryImage(fieldfield);
		findRegion(b);
		ADSTool.savePGM(outfilename, pgmOut);
	}
	
	public static void searchAreas(int [][] binImage) {
		
	}
	
	/**
	 * Converts an int Array into a two-dimensional Field
	 * @param sourceImage
	 * @return
	 */
	
	public static int[][] getField(int[] sourceImage) {

		// Create a new to-dimensional field with the size of the source image
		int[][] returnField = new int[sourceImage[1]][sourceImage[0]];

		// Set the counter to 3, to ignore the default values at the beginning
		// of the image-array (height, etc.)
		int sourceCounter = 3;

		// Write the image-file values to the two-dimensional array
		for (int i = 0; i < sourceImage[1]; i++) {
			for (int j = 0; j < sourceImage[0]; j++) {

				returnField[i][j] = sourceImage[sourceCounter];
				sourceCounter++;
			}
		}

		return returnField;

	}
	
	/**
	 * Converts an two-dimensional Field into an int Array
	 * @param targetField
	 * @param sourceImage
	 * @return
	 */
	
	public static int[]getArray(int[][]targetField,int[]sourceImage) {

		//Create a new array with the length of the original image-file
		int [] returnField = new int [(sourceImage[1]*sourceImage[0])+3];
		//Set the default Picture-Parameters
		returnField[0] = sourceImage[0];
		returnField[1] = sourceImage[1];
		returnField[2] = sourceImage[2];
		
		int p = 3;

		//Copy every image-value to the new image-file
			for(int i = 0;i<sourceImage[1];i++) {	
				for(int j = 0;j<sourceImage[0];j++) {
					returnField[p] = targetField[i][j];
					p++;
				}
			}

		return returnField;
		
	}
	

	private static boolean checkPixel(int a){
		if(a > 0)
			return true;
		else
			return false;
	}
	
	private static int setPixel(int a){
		return checkPixel(a) ? 1:0;
	}
	
	private static int[][] createBinaryImage(int[][] a){
		for(int i = 3; i < a[0][0]; i++)
			for(int j=0; j < a[0][1]; j++)
				a[i][j] = setPixel(a[i][j]);
		return a;
	}
	private static int[][] getFieldField(int[]a){
		int width = a[0];
		int height = a[1];
		//int[][]field = new int[width][height];
		int[][]field = ADSTool.feld2FeldFeld(a);
		/*for(int i=3; i < height+3;i++)
			for(int j=0; j < width; j++)
				field[i-3][j]=a[i];*/
		return field;
	}
	private static int[][] findRegion(int[][]a){
		int width = a[0][0];
		int height = a[0][1];
		int[][] o = new int[width][height];
		int count = 1;
		for(int i=3; i < width; i++){
			for(int j=0; j < height; j++){
				if(a[i][j]==1){
					int[][] e = new int[width][height];
					for(int k=-1; k <=1; k=k+2 ){
						for(int v=-1; v <= 1; v=v+2){
							e[(i+k)][j+v] = 1;
							e[i][j] = 1;
							if((a[i][j] & e[i][j]) == 1)
								o[i][j] = count;
						}
					}
				}
			}
			count++;
		}

		o[0][0] = width;
		o[0][1] = height;
		o[0][2] = count;
		return o;
	}
	private static int[] findRegion2(int[]a){
		int width = a[0];
		int height = a[1];
		int[] o = new int[a.length];
		int count = 1;
		for(int i=1; i < height; i++){
			if(a[i]==1){
				for(int j=1; j < width; j++){
					int[] e = new int[a.length];
					for(int k=-1; k <=1; k=k+2 ){
						for(int v=-1; v <= 1; v= v+2){
							e[(i+k)*width+(j+v)] = 1;
							e[i*width+j] = 1;
							if((a[i*width+j] & e[i*width+j]) == 1)
								o[i*width+j] = count;
						}
					}
				}
				count++;
			}
			
		}
		o[0] = width;
		o[1] = height;
		o[2] = count;
		return o;
	}
	
	private static void errorMsg() {
		System.out.println("////////////////////////////////////////////////////////////////");
		System.out.println("/                                                              /");
		System.out.println("/ 2 oder 1 Parameter wird erwartet:                            /");
		System.out.println("/ Param1 (inBild.pgm) [optional: outBild.pgm]                  /");
		System.out.println("/                                                              /");
		System.out.println("////////////////////////////////////////////////////////////////");
		System.exit(-1);
		
	}

}
